package model

import (
	"fmt"
	"github.com/jmoiron/sqlx"
	"time"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
)

type UsdtExamine struct {
	Id          int64 `json:"id" db:"id"`
	Uid         int64 `json:"uid" db:"uid"`
	LogId       int64 `json:"log_id" db:"log_id"`
	Operator    int64 `json:"operator" db:"operator"`
	Status      int   `json:"status" db:"status"`
	ExamineTime int64 `json:"examine_time" db:"examine_time"`
	CreateTime  int64 `json:"create_time" db:"create_time"`
	UpdateTime  int64 `json:"update_time" db:"update_time"`
}

type UsdtExamineDetail struct {
	UsdtExamine
	Amount   float64 `json:"amount" db:"amount"`
	Nickname string  `json:"nickname" db:"nickname"`
}

//插入记录
func AddUsdtExamine(db sqlx.Execer, data UsdtExamine) (err error) {
	data.Status = 3
	data.CreateTime = time.Now().Unix()
	data.UpdateTime = time.Now().Unix()
	_, err = db.Exec("INSERT INTO usdt_examine (uid,log_id,status,create_time,update_time) VALUES (?, ?, ?, ?, ?)",
		data.Uid, data.LogId, data.Status, data.CreateTime, data.UpdateTime)
	if err != nil {
		Log("err", err)
	}
	return err
}

//修改记录
func UpdateUsdtExamine(db sqlx.Execer, data UsdtExamine) (err error) {
	data.ExamineTime = time.Now().Unix()
	data.UpdateTime = time.Now().Unix()
	_, err = db.Exec("UPDATE usdt_examine SET status = ?, operator = ?, examine_time = ?, update_time = ? where id = ?",
		data.Status, data.Operator, data.ExamineTime, data.UpdateTime, data.Id)
	if err != nil {
		Log("err", err)
	}
	return err
}

//获取列表
func GetUsdtExamineList(db sqlx.Queryer, page, per, status int) (qi []UsdtExamineDetail, err error) {
	start := (page - 1) * per
	sqlStr := "SELECT t1.id,t1.uid,t1.log_id,t1.operator,t1.status,t1.examine_time,t1.create_time,t1.update_time,IFNULL(t2.amount,0) as amount,t3.nickname FROM usdt_examine t1 LEFT JOIN usdt_log t2 on log_id = t2.id LEFT JOIN user t3 on t1.uid = t3.uid "
	if status > 0 {
		sqlStr += fmt.Sprintf("WHERE t1.status = %d ", status)
	}
	sqlStr += fmt.Sprintf("ORDER BY t1.id DESC LIMIT %d,%d ", start, per)
	err = sqlx.Select(db, &qi, sqlStr)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//获取列表条目总数
func GetUsdtExamineListCount(db sqlx.Queryer, status int) (qi int, err error) {
	sqlStr := "SELECT count(*) FROM usdt_examine "
	if status > 0 {
		sqlStr += fmt.Sprintf("WHERE status = %d ", status)
	}
	err = sqlx.Get(db, &qi, sqlStr)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//获取单条记录：id
func GetUsdtExamine(db sqlx.Queryer, Id int64) (qi UsdtExamine, err error) {
	err = sqlx.Get(db, &qi, "SELECT id,uid,log_id,status,operator,examine_time,create_time,update_time FROM usdt_examine WHERE id = ?", Id)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//获取单条记录：log_id
func GetUsdtExamineByLogId(db sqlx.Queryer, logId int64) (qi UsdtExamine, err error) {
	err = sqlx.Get(db, &qi, "SELECT id,uid,log_id,status,operator,examine_time,create_time,update_time FROM usdt_examine WHERE log_id = ?", logId)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//是否存在审核中记录
func ExistUsdtExamine(db sqlx.Queryer, uid int64) (ok bool, err error) {
	var count int
	err = sqlx.Get(db, &count, "SELECT count(*) FROM usdt_examine WHERE uid = ? AND status = ?", uid, DealStatusCheck)
	if err != nil {
		Log("err", err)
		return false, err
	}
	if count == 0 {
		return false, nil
	}
	return true, nil
}

//查询待审核CVN金额信息
//Auth:Xu
func GetSumUsdtUnexamineAmount()(count float64, err error){
	err = sqlx.Get(config.C.DB.DB, &count, "select ifnull(-sum(l.amount),0) from usdt_examine e INNER JOIN usdt_log l ON e.log_id = l.id AND e.`status` = 3")
	if err != nil {
		Log("err", "查询USDT待审核金额信息失败:", err.Error())
	}
	return
}

//查询待审核CVN金额信息
//Auth:Xu
func GetSumUsdtExamineAmount()(count float64, err error){
	err = sqlx.Get(config.C.DB.DB, &count, "select ifnull(-sum(l.amount),0) from usdt_examine e INNER JOIN usdt_log l ON e.log_id = l.id AND e.`status` = 1")
	if err != nil {
		Log("err", "查询USDT待审核金额信息失败:", err.Error())
	}
	return
}
